﻿#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

namespace AI.Graph.Editor {


    public class EdgeContextEvent : GraphEvent {

        private GenericMenu m_menu;
        private ConnectionPort m_port1;
        private ConnectionPort m_port2;


        public override void OnInitialize () {
            canvas.onEdgeClicked += (from, to) => {
                m_port1 = from;
                m_port2 = to;
            };
        }


        public override void OnProcess (Event e) {
            if (m_menu != null) {
                m_menu.ShowAsContext ();
                m_menu = null;
            }

            if (m_port1 != null && m_port2 != null) {
                ConnectionPort port1 = m_port1;
                ConnectionPort port2 = m_port2;
                m_port1 = null;
                m_port2 = null;

                m_menu = new GenericMenu ();
                m_menu.AddItem (new GUIContent ("Delete Transition"), false, () => {
                    string recordTitle = "Connection removed: [" + port1.node.name + " : " + port1.name + "] -> [" + port2.node.name + " : " + port2.name + "]";
                    GraphUndo.RecordAndPerform (recordTitle,
                        () => { Serializer.DisconnectPorts (port1, port2); },
                        () => { Serializer.ConnectPorts (port1, port2); });
                });
            }
        }

    }


}
#endif